﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using System.Reflection;

namespace Tilver.DBConnectionUI
{
    public enum FilterMode 
    { 
        /// <summary>
        /// Only the listed providers will be visible
        /// </summary>
        Include, 
        /// <summary>
        /// the listed providers will not be available
        /// </summary>
        Exclude     
    }
    public class ConnectionStringDialog: CommonDialog
    {
        // ctr
        public ConnectionStringDialog()
        {
            this.Reset();
        }

        /// <summary>
        /// If true, the control will test for valid connections.
        /// (Default = True)
        /// </summary>
        public bool ValidateConnections { get; set; }

        /// <summary>
        /// Gets or sets the DataProvider invariant Name of the control
        /// if null or empty it will display the list of available providers
        /// </summary>
        public string Provider { get; set; }

        /// <summary>
        /// Gets or sets the connection string
        /// </summary>
        public string ConnectionString { get; set; }

        /// <summary>
        /// Returns the DbProviderFactory instance for the selected provider
        /// or null if the selected provider don't support factories.
        /// </summary>
        /// <returns>System.Data.Common.DbProviderFactory</returns>
        public DbProviderFactory GetFactory()
        {
            if (!string.IsNullOrEmpty(Provider))
            {
                DbProvider prov = Providers[Provider];
                if (prov != null)
                    return prov.GetFactory();
            }
            return null;
        }

        /// <summary>
        /// Returns an initialized IDBConnection for the selected provider
        /// </summary>
        /// <returns>System.Data.IDbConnection</returns>
        public IDbConnection GetConnection()
        {
            if (!string.IsNullOrEmpty(Provider))
            {
                DbProvider prov = Providers[Provider];
                if (prov != null)
                {
                    IDbConnection conn = prov.GetConnection();
                    if (conn != null)
                    {
                        conn.ConnectionString = this.ConnectionString;
                        return conn;
                    }
                }
            }
            return null;            
        }

        protected override bool RunDialog(IntPtr hwndOwner)
        {
            frmDialog frm = new frmDialog(this);
            if (frm.ShowDialog() == DialogResult.OK)
                return true;
            else
                return false;
        }

        public override void Reset()
        {
            this.Provider = "";
            this.ConnectionString = "";
            this.ValidateConnections = true;
        }

        #region Static

        /// <summary>
        /// Gets the collection of filters
        /// </summary>
        public static List<string> Filters 
        {
            get
            {
                if (filters == null)
                    filters = new List<string>();
                return filters;
            }
        }
        static List<string> filters;

        /// <summary>
        /// Gets or Sets the filtering mode
        /// </summary>
        public static FilterMode FilterMode { get; set; }

        /// <summary>
        /// Gets the collection of available DataProviders
        /// </summary>
        internal static DbProvidersCollection Providers
        {
            get
            {
                if (_providers == null)
                    _providers = new DbProvidersCollection();
                return _providers;
            }
        }
        static DbProvidersCollection _providers;

        internal static UDLEditorsCollection Editors
        {
            get
            {
                if (_editors == null)
                    _editors = new UDLEditorsCollection();
                return _editors;
            }
        }
        static UDLEditorsCollection _editors;

        public static void RegisterEditor(Type editorType, string providerInvariantName)
        {
            Editors.Add(providerInvariantName, editorType);
        }

        public static void RegisterProvider(Type providerType, string invariantName, string description)
        {
            DbProvider prov = new DbProvider(invariantName, description, providerType);
            Providers.Add(prov);
        }

        #endregion

    }
}
